package com.smh.a02ListDemo2;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class ArrayListDemo1 {
    public static void main(String[] args) {

        List<String> list = new ArrayList<>();
        list.add("asd");
        list.add("qwe");
        list.add("zxc");
        list.add("fgh");
        list.add("vbn");
        list.add(0,"smh");//指定添加位置
        list.remove(0);//根据索引删除
        list.remove("asd");//根据元素删除
        System.out.println(list.set(0, "111"));//返回修改的元素
        System.out.println(list.get(0));
        System.out.println("---------------");
        //增强for
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println("---------------");
// foreach
        list.forEach(s -> {
            System.out.println(s);
        });
        System.out.println("---------------");
//迭代器
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

        System.out.println("---------------");
//for循环
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        System.out.println("------------------");
        //列表迭代器
        ListIterator<String> listIterator = list.listIterator();
        while (listIterator.hasNext()){
            listIterator.add("4577");//放在当前指针的位置.数据后移
            String s = listIterator.next();//先获取数据,在指针后移
            System.out.println(s);
            if (s.equals("zxc")){
                listIterator.remove();
            }

        }

        System.out.println("---------------");
//for循环
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }


        System.out.println("------------------");

        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        list1.add(4);
        //当方法出现了重载现象,会优先调用实参与形参一致的哪个方法;

        list1.remove(1);
        Integer i = 1;
        list1.remove(i);


    }
}
